var jsApp	=
{
	/* ---

		Initialize the jsApp

		---			*/
	onload: function()
	{
		// init the video
		if (!me.video.init('jsapp', WIDTH, HEIGHT, false, 1.0))
		{
			alert("Sorry but your browser does not support html 5 canvas.");
         return;
		}

		// initialize the "audio"
		me.audio.init("mp3,ogg");

		// set all resources to be loaded
		me.loader.onload = this.loaded.bind(this);

		// set all resources to be loaded
		me.loader.preload(g_resources);

		// load everything & display a loading screen
		me.state.change(me.state.LOADING);

		// Render collisions
		// me.debug.renderHitBox = true;
	},


	/* ---

		callback when everything is loaded

		---										*/
	loaded: function ()
	{
		// set the "Play/Ingame" Screen Object
		me.state.set(me.state.MENU, new TitleScreen());
	
		// set the "Play/Ingame" Screen Object
		me.state.set(me.state.PLAY, new PlayScreen());

		// set a global fading transition for the screen
		me.state.transition("fade", "#000000", 250);
	
		// add object entities in the entity pool
		me.entityPool.add("changeLevelEntity", changeLevelEntity);
		me.entityPool.add("mainPlayer", PlayerEntity);
		me.entityPool.add("mutobj_seed", Seed);
		// me.entityPool.add("startL", CustomLoadingScreen);
		
		// enable the keyboard
		me.input.bindKey(me.input.KEY.LEFT,  "left");
		me.input.bindKey(me.input.KEY.RIGHT, "right");
		me.input.bindKey(me.input.KEY.UP, "up");
		me.input.bindKey(me.input.KEY.DOWN, "down");
		me.input.bindKey(me.input.KEY.X, "jump", true);
		me.input.bindKey(me.input.KEY._1, "timePast", true);
		me.input.bindKey(me.input.KEY._2, "timePresent", true);
		me.input.bindKey(me.input.KEY._3, "timeFuture", true);
		me.input.bindKey(me.input.KEY.A, "alertPosition", true);
		me.input.bindKey(me.input.KEY.M, "mute", true);
		// me.input.bindKey(me.input.KEY.P, "pause", true);
		me.input.bindKey(me.input.KEY.SPACE, "CollectPutDown", true);
		
		// start the game
		// me.state.change(me.state.PLAY);
		
		// display the menu title
		me.state.change(me.state.MENU);
	}
}; // jsApp

/* the in game stuff*/
var PlayScreen = me.ScreenObject.extend(
{
   onResetEvent: function()
	{
        // load a level
		var nameLvl = "level_" + currentLevel + "_" + timeArray[currentTime];
		// alert("name Lvl : " + nameLvl);
        // me.levelDirector.loadLevel(nameLvl);
		var newLevelScreen = new StartLevelScreen(currentLevel, true);
		me.game.add(newLevelScreen, 999);
		me.game.sort();
		currentTime = PRESENT;
		me.game.viewport.fadeOut('#000000', 400, function() {
			isChangingLevel = false;
		});
		
        // make sure everyhting is in the right order
        me.game.sort();
		
	},


	/* ---

		 action to perform when game is finished (state change)

		---	*/
	onDestroyEvent: function()
	{
		// remove the HUD
		me.game.disableHUD();
	}

});


//bootstrap :)
window.onReady(function()
{
	jsApp.onload();
});